/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jlxy.ise.eroad.service;

/**
 *
 * @author hcc25_000
 */
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import jlxy.ise.eroad.controller.Bean.CompeteBean;
import jlxy.ise.eroad.controller.Bean.WaitBean;
import jlxy.ise.eroad.util.SqlOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author hcc25_000
 */
@Service
@Transactional
public class DriverService {
    private int flag = 0;//司机竞价时的标志位
    private List res=null;
    private List list=new LinkedList();
    @Autowired
    private SqlOperator sql;

    public void setInfo(CompeteBean cb) {
        String driver_name = cb.getDriver_name();
        int price = cb.getPrice();
        String cdkey = cb.getCdkey();
        String state = "no";
        String stmt = "insert into driver_compete(driver_name,cdkey,price,state) values(?,?,?,?)";

        sql.insert(stmt, driver_name, cdkey, price, state);
    }

    public List getInfo(String state) {
        String stmt = "select * from driver_compete where state=?";

        List result = sql.query(stmt, state);
        return result;
    }

    public static void temp(Map m1, Map m2) {
        Map temp = new HashMap();
        temp.put("driver_name", m1.get("driver_name"));
        temp.put("cdkey", m1.get("cdkey"));
        temp.put("price", m1.get("price"));
        temp.put("state", m1.get("state"));
        temp.put("id", m1.get("id"));

        m1.replace("driver_name", m2.get("driver_name"));
        m1.replace("cdkey", m2.get("cdkey"));
        m1.replace("price", m2.get("price"));
        m1.replace("state", m2.get("state"));
        m1.replace("id", m2.get("id"));

        m2.replace("driver_name", temp.get("driver_name"));
        m2.replace("cdkey", temp.get("cdkey"));
        m2.replace("price", temp.get("price"));
        m2.replace("state", temp.get("state"));
        m2.replace("id", temp.get("id"));
    }

    public List startCompete(CompeteBean cb) {
        list=new LinkedList();
        setInfo(cb);
        flag++;
        if(flag==1){
            try{
                Thread.sleep(30000);//此处新建线程，调用线程的run
                res=getInfo("no");
                for(int i=0;i<res.size()-1;i++){
                    Map m1=(Map)res.get(i);
                    for(int j=i+1;j<res.size();j++){
                        Map m2=(Map)res.get(j);
                        if((int)m1.get("price")>(int)m2.get("price")){
                            temp(m1,m2);
                        }
                    }
                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            list.add((Map)res.get(0));
        }else{
            try{
                Thread.sleep(30000);//设置定时器timer
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        //System.out.println(res);
        return list;
    }
}
